//
//  FullCakeViewController.m
//  Konditor Meister
//
//  Created by Apple on 20/11/12.
//  Copyright (c) 2012 Apple. All rights reserved.
//

#import "FullCakeViewController.h"
#define NUMBERCAKES 8

@interface FullCakeViewController ()

@end

@implementation FullCakeViewController

@synthesize currentImage,strName;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


-(IBAction)leftNav:(id)sender
{
    if(indexNum>0)
    {
        indexNum--;
        [self  setimageForscrll];

    }
    
//    if(currentImage>1)
//    {
//        currentImage--;
//        imagebig.image = [UIImage imageNamed:[NSString stringWithFormat:@"base%d",currentImage]];
//        imageDown.image = [UIImage imageNamed:[NSString stringWithFormat:@"base%d_small",currentImage]];
//    }
}

-(IBAction)rightNav:(id)sender
{
    if(indexNum <7)
    {
    indexNum++;
    [self  setimageForscrll];
    }
//    if(currentImage < 8)
//    {
//        currentImage++;
//        imagebig.image = [UIImage imageNamed:[NSString stringWithFormat:@"base%d",currentImage]];
//        imageDown.image = [UIImage imageNamed:[NSString stringWithFormat:@"base%d_small",currentImage]];
//
//    }
}

-(void)setimageForscrll
{
    switch (indexNum) {
        case 0:
        {
            imageDown.image = [UIImage imageNamed:@"cake_bottom_nav"];
            self.strName = @"cake";
            [self  setScroll:7];
        }
            break;
        case 1:
        {
            imageDown.image = [UIImage imageNamed:@"cake_bottom_nav"];

            self.strName = @"cake";
            [self  setScroll:7];
        }
            break;
        case 2:
        {
            imageDown.image = [UIImage imageNamed:@"cake_bottom_nav"];

            self.strName = @"cake";
            [self  setScroll:7];
        }
            break;
        case 3:
        {
            imageDown.image = [UIImage imageNamed:@"decorations_bottom_nav"];

            self.strName = @"decorations";
            [self  setScroll:39];
        }
            break;
        case 4:
        {
            imageDown.image = [UIImage imageNamed:@"flower_bottom_nav_03"];

            self.strName = @"flowers";
            [self  setScroll:20];
        }
            break;
        case 5:
        {
            imageDown.image = [UIImage imageNamed:@"jewelry_bottom_nav"];
            
            self.strName = @"jewelry";
            [self  setScroll:9];
        }
            break;
        case 6:
        {
            imageDown.image = [UIImage imageNamed:@"ribbon_bottom_nav"];
            
            self.strName = @"ribbons";
            [self  setScroll:12];
        }
            break;
        case 7:
        {
            imageDown.image = [UIImage imageNamed:@"topper_bottom_nav"];
            
            self.strName = @"toppers";
            [self  setScroll:28];
        }
            break;
            
        default:
            break;
    }
}
-(IBAction)back:(id)sender
{
    [self.navigationController  popViewControllerAnimated:YES];
}


- (void)viewDidLoad
{
    indexNum = 0;
    imageCake.image = [UIImage imageNamed:[NSString stringWithFormat:@"cake%d",1]];
    
    arrayCakes = [[NSMutableArray  alloc] init];
    
       
    
//    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
//    [panRecognizer setMinimumNumberOfTouches:1];
//    [panRecognizer setMaximumNumberOfTouches:1];
//    [panRecognizer setDelegate:self];
//    [imagebig addGestureRecognizer:panRecognizer];

//    [imageCake2 addGestureRecognizer:panGesture];
//    [imageCake3 addGestureRecognizer:panGesture];
//    [imageCake4 addGestureRecognizer:panGesture];

//    [self addPan:imageCake];
//    [self addPan:imageCake2];
//    [self addPan:imageCake3];
//    [self addPan:imageCake4];

    imageCake.userInteractionEnabled = YES;
    
    imagebig.image = [UIImage imageNamed:[NSString stringWithFormat:@"base%d",currentImage]];
    imageDown.image = [UIImage imageNamed:[NSString stringWithFormat:@"base%d_small",currentImage]];
    self.strName = @"cake";
    [self  setScroll:7];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)setScroll:(NSInteger)totNum
{
    counter = 0;
    
    CGRect frameScrl = [scrollCakes   frame];
    [scrollCakes removeFromSuperview];
    
    scrollCakes = [[UIScrollView  alloc] initWithFrame:frameScrl];
    [self.view  addSubview:scrollCakes];
    [scrollCakes setContentSize:CGSizeMake(320, totNum/4*70)];
    int incX = 10;
    int incY = 10;
//    for(int i=0;i< 2;i++)
    while (1)
    {
        for(int j=0;j<4;j++)
        {
            
            UIImageView *imageCake1 = [[UIImageView  alloc] initWithFrame:CGRectMake(incX, incY, 70  , 55)];
            imageCake1.userInteractionEnabled = YES;
            imageCake1.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_%d.png",self.strName,++counter] ];
            imageCake1.tag = counter;
            imageCake1.backgroundColor = [UIColor  colorWithRed:((float)245)/256 green:((float)221)/256 blue:((float)221)/256 alpha:1];
            
           

            if(counter == totNum)
            {
                break;
            }
            UITapGestureRecognizer *gesture = [[UITapGestureRecognizer  alloc] init];
            [imageCake1  addGestureRecognizer:gesture];
            [gesture addTarget:self
                        action:@selector(tapimage:)];
            
            [scrollCakes  addSubview:imageCake1];
            incX += 80;
        }
        if(counter == totNum)
        {
            break;
        }
        incX = 10;
        incY += 60;
        
    }
    

}

-(IBAction)plus:(id)sender
{
    tempImage.frame = CGRectMake(tempImage.frame.origin.x, tempImage.frame.origin.y, tempImage.frame.size.width+10, tempImage.frame.size.height+10);
}
-(IBAction)minus:(id)sender
{
    tempImage.frame = CGRectMake(tempImage.frame.origin.x, tempImage.frame.origin.y, tempImage.frame.size.width-10, tempImage.frame.size.height-10);
}
-(IBAction)clearall:(id)sender
{
    [tempImage   removeFromSuperview];
}

-(void)addPan:(UIImageView *)imageV
{
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
    [panGesture setMaximumNumberOfTouches:2];
    [panGesture setDelegate:self];
    [imageV addGestureRecognizer:panGesture];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                               initWithTarget:self
                                               action:@selector(handleLongPress:)];
    longPress.minimumPressDuration = 1.0;
    [imageV addGestureRecognizer:longPress];
    [longPress release];
}



- (void)handleLongPress:(UILongPressGestureRecognizer*)sender 
{
    if (sender.state == UIGestureRecognizerStateEnded) 
    {       
         UIImageView *oldIamge = tempImage;
        
        oldIamge.layer.borderWidth = 0.0;
        oldIamge.layer.borderColor = [[UIColor  whiteColor] CGColor];
        
        tempImage = (UIImageView *)[sender  view];
        
        tempImage.layer.borderWidth = 2.0;
        tempImage.layer.borderColor = [[UIColor  redColor] CGColor];
        
//        tempImage.image =  [FullCakeViewController  colorizeImage:tempImage.image color:[UIColor  magentaColor]];
    }
    else {
        NSLog(@"Long press detected.");
    }
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    tempImage.layer.borderWidth = 0.0;
    tempImage.layer.borderColor = [[UIColor  clearColor] CGColor];

    tempImage = nil;
}
-(void)tapimage:(UITapGestureRecognizer *)gesture
{
    scrollCakes.hidden = YES;
    
    UIImageView *imagetap = [[UIImageView  alloc] initWithFrame:CGRectMake(rand()%320, rand()%420, 50, 50)];
    imagetap.userInteractionEnabled = YES;
    [self addPan:imagetap];
    imagetap.image = [(UIImageView *)[gesture  view] image];
    [self.view  addSubview:imagetap ];
     
    
//    if(imageNum == -1)
//        tempImage.image =  [UIImage imageNamed:[NSString stringWithFormat:@"cake%d",[gesture view].tag]];
////    UIEvent
//    switch (imageNum) {
//        case 0:
//        {
//            imageCake.image =  [UIImage imageNamed:[NSString stringWithFormat:@"cake%d",[gesture view].tag]];
//            imageNum =1;
//        }
//            break;
//        case 1:
//        {
//            imageCake2.image =  [UIImage imageNamed:[NSString stringWithFormat:@"cake%d",[gesture view].tag]];
//            imageNum = 2;
//            break;
//        }
//
//        case 2:
//        {
//            imageCake3.image =  [UIImage imageNamed:[NSString stringWithFormat:@"cake%d",[gesture view].tag]];
//            imageNum = 3;
//            break;
//        }
//        case 3:
//        {
//            imageCake4.image =  [UIImage imageNamed:[NSString stringWithFormat:@"cake%d",[gesture view].tag]];
//            imageNum = -1;
//            break;
//        }
//        default:
//            break;
//    }
    

}

+ (UIImage *)colorizeImage:(UIImage *)baseImage color:(UIColor *)theColor {
    UIGraphicsBeginImageContext(baseImage.size);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);
    
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    
    CGContextSaveGState(ctx);
    CGContextClipToMask(ctx, area, baseImage.CGImage);
    
    [theColor set];
    CGContextFillRect(ctx, area);
    
    CGContextRestoreGState(ctx);
    
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    
    CGContextDrawImage(ctx, area, baseImage.CGImage);
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return newImage;
}

#pragma pangesture code

- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
    UIView *piece = [gestureRecognizer view];
    
    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
    
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
        
        [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
        [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
    }
}

- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        UIView *piece = gestureRecognizer.view;
        CGPoint locationInView = [gestureRecognizer locationInView:piece];
        CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];
        
        [gestureRecognizer  view].layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
        piece.center = locationInSuperview;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
